package com.lanou3g.zjl.chating.home.friends;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMGroup;
import com.lanou3g.zjl.chating.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by dllo on 15/11/22.
 */
//listview的适配器
public class MyexpandableListAdapter extends BaseExpandableListAdapter {
    private ArrayList<String> groupList;
    private Context context;
    private LayoutInflater inflater;
    private ArrayList<List<EMGroup>> childList;
    private List<EMGroup> myList;
    private List<EMGroup> joinList;
    public MyexpandableListAdapter(Context context,List<EMGroup> data) {
        this.context = context;
        inflater = LayoutInflater.from(context);

        groupList = new ArrayList<>();
        myList = new ArrayList<>();
        joinList = new ArrayList<>();

        groupList.add("我创建的群组");
        groupList.add("我加入的群组");
        for(EMGroup e : data){
            if(e.getOwner().equals(EMChatManager.getInstance().getCurrentUser())){
                myList.add(e);
            }else {
                joinList.add(e);
            }
        }
        childList = new ArrayList<List<EMGroup>>();
        childList.add(myList);
        childList.add(joinList);

    }
    //刷新列表数据的方法
    public void refreshList(List<EMGroup> data){
        ArrayList<List<EMGroup>> childList = new ArrayList<>();
        List<EMGroup> myList = new ArrayList<>();
        List<EMGroup> joinList = new ArrayList<>();
        for(EMGroup e : data){
            if(e.getOwner().equals(EMChatManager.getInstance().getCurrentUser())){
                myList.add(e);
            }else {
                joinList.add(e);
            }
        }
        childList.add(myList);
        childList.add(joinList);
        this.childList = childList;
        notifyDataSetChanged();
    }
    // 返回父列表个数
    @Override
    public int getGroupCount() {

        return groupList.size();
    }

    // 返回子列表个数
    @Override
    public int getChildrenCount(int groupPosition) {

        return childList.get(groupPosition).size();
    }

    @Override
    public String getGroup(int groupPosition) {

        return groupList.get(groupPosition);
    }

    @Override
    public EMGroup getChild(int groupPosition, int childPosition) {

        return childList.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {

        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {

        return childPosition;
    }

    @Override
    public boolean hasStableIds() {

        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupHolder groupHolder = null;
        if(convertView == null)
        {
            convertView = inflater.inflate(R.layout.groupview,null);

            groupHolder = new GroupHolder(convertView);

            convertView.setTag(groupHolder);
        }else {
            groupHolder = (GroupHolder) convertView.getTag();
        }
        groupHolder.groupClass.setText(getGroup(groupPosition));
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {
        ChildHolder childHolder = null;
        if(convertView == null){
            convertView = inflater.inflate(R.layout.groups_listitem,null);
            childHolder = new ChildHolder(convertView);
            convertView.setTag(childHolder);
        }else {
            childHolder = (ChildHolder) convertView.getTag();
        }
        childHolder.groupName.setText(getChild(groupPosition,childPosition).getGroupName());
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {

        return true;
    }

    static class GroupHolder {

        @Bind(R.id.groupClass)
        TextView groupClass;

        GroupHolder(View view) {

            ButterKnife.bind(this, view);
        }
    }

    static class ChildHolder {
        @Bind(R.id.childGroupName)
        TextView groupName;
        @Bind(R.id.iv)
        ImageView iv;
        ChildHolder(View view) {

            ButterKnife.bind(this, view);
        }
    }
}
